﻿Imports System.Web.Mvc
Imports System.IO
Imports System.Collections.Specialized

Public Interface iJSonDataCrud
    Function ReadData(ByVal Skip As Integer, ByVal Take As Integer, ByVal SortDir As String, ByVal Sort As String, ByVal param As String) As JsonResult
    Function SaveData(ByVal param As NameValueCollection) As JsonResult
    Function UpdateData(ByVal param As NameValueCollection) As JsonResult
    Function DeleteData(ByVal param As NameValueCollection) As JsonResult

End Interface
Public MustInherit Class JSonDataCrud
    Implements iJSonDataCrud

    Public MustOverride Function ReadData(ByVal Skip As Integer, ByVal Take As Integer, ByVal SortDir As String, ByVal Sort As String, ByVal param As String) As System.Web.Mvc.JsonResult Implements iJSonDataCrud.ReadData

    Public MustOverride Function SaveData(ByVal param As NameValueCollection) As System.Web.Mvc.JsonResult Implements iJSonDataCrud.SaveData

    Public MustOverride Function DeleteData(ByVal param As NameValueCollection) As System.Web.Mvc.JsonResult Implements iJSonDataCrud.DeleteData

    Public MustOverride Function UpdateData(ByVal param As NameValueCollection) As System.Web.Mvc.JsonResult Implements iJSonDataCrud.UpdateData

End Class

Public Class CRUDHelper
    Shared mListStore As ListDataAcess
    Shared Sub InitThis()
        Dim sr As StreamReader = New StreamReader(AppDomain.CurrentDomain.BaseDirectory & "Models\DataAccessList.txt")
        Dim cdata As String
        Dim arr() As String
        mListStore = New ListDataAcess
        Do While sr.Peek() >= 0
            cdata = sr.ReadLine()
            arr = cdata.Split(";")
            mListStore.AddItem(arr(0), New DataObj With {.FileName = arr(1), .ClassName = arr(2)})
        Loop
        sr.Close()

    End Sub

    Shared Function ReadDataObject(ByVal Skip As Integer, ByVal Take As Integer, ByVal SortDir As String, ByVal Sort As String, ByVal Target As String, ByVal param As String) As JsonResult
        Dim x As DataObj
        If mListStore Is Nothing Then
            InitThis()
        End If
        x = mListStore.Item(Target)
        If Not x Is Nothing Then
            Dim xr As JSonDataCrud
            xr = Geto(x)
            Return xr.ReadData(Skip, Take, SortDir, Sort, param)
        Else
            Return Nothing
        End If
    End Function
    Shared Function SaveDataObject(ByVal Target As String, ByVal param As NameValueCollection) As JsonResult
        Dim x As DataObj
        If mListStore Is Nothing Then
            InitThis()
        End If
        x = mListStore.Item(Target)
        If Not x Is Nothing Then
            Dim xr As JSonDataCrud
            xr = Geto(x)
            Return xr.SaveData(param)
        Else
            Return Nothing
        End If
    End Function
    Shared Function UpdateDataObject(ByVal Target As String, ByVal param As NameValueCollection) As JsonResult
        Dim x As DataObj
        If mListStore Is Nothing Then
            InitThis()
        End If
        x = mListStore.Item(Target)
        If Not x Is Nothing Then
            Dim xr As JSonDataCrud
            xr = Geto(x)
            Return xr.UpdateData(param)
        Else
            Return Nothing
        End If
    End Function
    Shared Function DeleteDataObject(ByVal Target As String, ByVal param As NameValueCollection) As JsonResult
        Dim x As DataObj
        If mListStore Is Nothing Then
            InitThis()
        End If
        x = mListStore.Item(Target)
        If Not x Is Nothing Then
            Dim xr As JSonDataCrud
            xr = Geto(x)
            Return xr.DeleteData(param)
        Else
            Return Nothing
        End If
    End Function
    Shared Function Geto(ByVal dta As DataObj) As JSonDataCrud
        Dim currDomain As AppDomain
        Dim TblData As JSonDataCrud
        Dim objHandle As System.Runtime.Remoting.ObjectHandle
        Try

            currDomain = AppDomain.CurrentDomain
            objHandle = currDomain.CreateInstanceFrom(AppDomain.CurrentDomain.BaseDirectory & "bin\" & dta.FileName, dta.ClassName)
            TblData = objHandle.Unwrap()
            Return TblData

        Catch e As Exception
            Return Nothing
        End Try
    End Function
End Class

Public Class ListDataAcess
    Inherits System.Collections.Specialized.NameObjectCollectionBase
    Dim _DataObj As DataObj
    Public Sub AddItem(ByVal name As String, ByVal data As DataObj)
        Me.BaseAdd(name, data)
    End Sub
    ' Gets a key-and-value pair (DictionaryEntry) using an index.
    Default Public ReadOnly Property Item(ByVal index As Integer) As DataObj
        Get

            Return Me.BaseGet(index)
        End Get
    End Property
    ' Gets a key-and-value pair (DictionaryEntry) using an index.
    Default Public ReadOnly Property Item(ByVal index As String) As DataObj
        Get
            Try
                Return Me.BaseGet(index)
            Catch ex As Exception
                Return Nothing
            End Try

        End Get
    End Property
End Class

Public Class DataObj
    Dim mtbl As String
    Dim mSource As String
    Dim mFile As String
    Dim _TableName As String
    Public Property TableName() As String
        Get
            Return _TableName
        End Get
        Set(ByVal value As String)
            _TableName = value
        End Set
    End Property
    Dim _FileName As String
    Public Property FileName() As String
        Get
            Return _FileName
        End Get
        Set(ByVal value As String)
            _FileName = value
        End Set
    End Property

    Dim _ObjName As String
    Public Property ClassName() As String
        Get
            Return _ObjName
        End Get
        Set(ByVal value As String)
            _ObjName = value
        End Set
    End Property
End Class



